package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonInterface;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IRoleDao;
import com.magicode.cms.entiy.base.Role;
import com.magicode.cms.service.IRoleService;

@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role,IRoleDao>  implements IRoleService,UtilPath,JsonInterface {

	@Autowired
	@Qualifier("daorole")
	private IRoleDao roleDao;
	public IRoleDao getDao() throws Exception {
		return roleDao;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".name as name ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.Role.class.getName()+" "+entity_alias_name+"";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" where "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "name");
		map.put(json_title, "名称");
		beaninfo.add(map);
		return beaninfo;
	}
	public IRoleDao getRoleDao() {
		return roleDao;
	}
	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}
	
	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation>();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("设置权限");
		opt.setUrl(ctx.getContextPath()+"/operate/setPermission?roleId="+id+"");
		opt.setType("setPermission");
		opts.add(opt);
		return opts;
	}

	@Autowired
	private ServletContext ctx;
	public ServletContext getCtx() {
		return ctx;
	}
	public void setCtx(ServletContext ctx) {
		this.ctx = ctx;
	}
	
}
